﻿using Bll;
using Entity;
using Model;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Vote.Models;

namespace Bll
{
    public class RedisCommon
    {
        /// <summary>
        /// Redis新增集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="tableName"></param>
        public static void InsertListToRedis<T>(string sql, string tableName) where T : class
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                List<T> listVo = new List<T>();
                listVo = BllVoteNumber.SelectEntityBySql<T>(tableName, sql);
                if (listVo != null && listVo.Count > 0)
                {
                    var vs = redisClient.GetTypedClient<T>();
                    vs.DeleteAll();
                    var vsToStore = listVo;
                    vs.StoreAll(vsToStore);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis新增集合失败：" + sql + "||" + ex.Message);
            }
            finally
            {
                Release(redisClient);
            }
        }
        /// <summary>
        /// 通过Redis查询集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetListByRedis<T>() where T : class
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                var Common = redisClient.GetTypedClient<T>();
                return Common.GetAll().ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis查询失败：" + ex.Message);
                return null;
            }
            finally
            {
                Release(redisClient);
            }
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageModel"></param>
        public static void GetPageListByRedis<T>(ref PageModel<T> pageModel) where T : class
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                var Common = redisClient.GetTypedClient<T>();
                List<T> temp = Common.GetAll().ToList();
                pageModel.Total = temp.Count();
                pageModel.ObjectList = temp.Take(pageModel.PageSize * pageModel.PageIndex).Skip(pageModel.PageSize * (pageModel.PageIndex - 1)).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis分页查询失败：" + ex.Message);
            }
            finally
            {
                Release(redisClient);
            }
        }

        /// <summary>
        /// 通过场次id获取VoteShowModel的分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageModel"></param>
        /// <param name="VoteNumberId"></param>
        public static void GetPageListByRedisConditions<T>(ref PageModel<VoteShowModel> pageModel, int VoteNumberId, string userName, int limitVoteNum)
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                List<VoteShowModel> temp = new List<VoteShowModel>();
                List<VoteShowModel> tempStr = new List<VoteShowModel>();
                List<VoteShowModel> tempStr1 = new List<VoteShowModel>();
                var Common = redisClient.GetTypedClient<VoteShowModel>();
                if (VoteNumberId == -1)
                {
                    temp = Common.GetAll().OrderBy(t => t.VoteNumberId).ThenByDescending(t => t.VoteNum).ToList();
                }
                else
                {
                    #region 从数据库取出票数合并到redis查出来的VoteShowModel集合
                    tempStr = Common.GetAll().Where(t => t.VoteNumberId == VoteNumberId).ToList();
                    List<CanVoteModel> cvm = BllVoteNumber.GetVotesByProceDure(VoteNumberId, userName, limitVoteNum);
                    if (tempStr.Count > 0 && cvm.Count > 0)
                    {
                        foreach (var listvote in tempStr)
                        {
                            foreach (var voteDb in cvm)
                            {
                                if (voteDb.showId == listvote.Id)
                                {
                                    listvote.VoteNum = voteDb.voteNum;
                                    listvote.canVoteType = voteDb.canVoteType;
                                    tempStr1.Add(listvote);
                                }
                            }
                        }
                    }
                  //  temp = tempStr1.OrderByDescending(t => t.VoteNum).ToList();
                    temp = tempStr1.OrderBy(t => t.Identify).ToList();
                    #endregion
                }
                pageModel.Total = temp.Count();
                pageModel.ObjectList = temp.Take(pageModel.PageSize * pageModel.PageIndex).Skip(pageModel.PageSize * (pageModel.PageIndex - 1)).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis分页查询失败：" + ex.Message);
            }
            finally
            {
                Release(redisClient);
            }
        }
        /// <summary>
        /// 获取没有过期的场次信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<VoteNumber> GetVoteNumberByRedis()
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                DateTime date = DateTime.Now.ToString("yyyy-MM-dd").ConToDateTime(DateTime.Now); //2017-04-28
                var Common = redisClient.GetTypedClient<VoteNumber>();
                return Common.GetAll().Where(x => x.StartTime <= date && x.EndTime >= date).ToList();
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis查询失败：" + ex.Message);
                return null;
            }
            finally
            {
                Release(redisClient);
            }
        }
        /// <summary>
        /// 获取单个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetSimpleByRedis<T>(string name) where T : class
        {
            IRedisClient redisClient = RedisManager.GetClient();
            try
            {
                var Common = redisClient.GetTypedClient<T>();
                return Common.GetValue(name);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("Redis查询单个值失败：" + ex.Message);
                return null;
            }
            finally
            {
                Release(redisClient);
            }
        }

        private static void Release(IRedisClient redisClient)
        {
            //RedisNativeClient redisNativeClient = (RedisNativeClient)redisClient;
            //redisNativeClient.IdleTimeOutSecs = 60;
            //redisNativeClient.ClientKill(redisClient.Host + ":" + redisClient.Port);
            //RedisManager.prcm.DisposeClient(redisNativeClient);

            redisClient.Dispose();
        }
    }
}
